

const util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeType: 0,
    address_get_type: -1,
    address_order_flag: false,
    addrBoList: []
  },
  getInit() {
    this.setData({
      addrBoList: []
    })
    util.https('addrMgmt/addrQry', {
      "channel": "1",
      type: this.data.activeType
    }, (res) => {
      console.log(res)
      this.setData({
        addrBoList: res.addrBoList
      })
    }, (res) => {
      console.log(res)
      wx.showModal({
        title: '温馨提示',
        content: res.errorMsg,
        confirmColor: '#F08302',
        showCancel: false,
        success(res) {
        }
      })

    })
  },
  onChangeType(e) {
    this.setData({
      activeType: e.detail.index
    })
    wx.setStorageSync('addressListTabIndex', e.detail.index)
    this.getInit();
  },
  handleDel(e) {
    let item = e.currentTarget.dataset.item;
    console.log(item)
    const _this = this;
    wx.showModal({
      title: '温馨提示',
      content: '确定删除吗？',
      confirmColor: '#F08302',
      showCancel: true,
      success(res) {
        if (res.confirm) {
          util.https('addrMgmt/addrDel', {
            "id": item.id
          }, (res) => {
            console.log(res)
            wx.showModal({
              title: '温馨提示',
              content: '删除成功',
              confirmColor: '#F08302',
              showCancel: false,
              success(res) {
              }
            })
            _this.getInit();
          }, (res) => {
            console.log(res)
            wx.showModal({
              title: '温馨提示',
              content: res.errorMsg,
              confirmColor: '#F08302',
              showCancel: false,
              success(res) {
              }
            })
          })
        }

      }
    })

  },
  handleEdit(e) {
    let item = e.currentTarget.dataset.item;
    console.log(item)
    wx.setStorageSync('addressedit', item);
    wx.setStorageSync('addressType', this.data.activeType);
    wx.navigateTo({ url: '/page5/addressedit/index' })
  },
  doAdd() {
    wx.setStorageSync('addressedit', '');
    wx.setStorageSync('addressType', this.data.activeType);
    wx.navigateTo({ url: '/page5/addressedit/index' })
  },
  handleItem(e) {
    if (this.data.address_get_type == -1) {
      return
    }
    let item = e.currentTarget.dataset.item;
    console.log(item)
    wx.setStorageSync(
      'address_get',
      item
    );
    wx.navigateBack({
      delta: 1
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  onBackPress(options) {
    console.log(options)
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // wx.setStorageSync('addressEditMap', '');

    wx.getStorage({
      key: 'address_get_type',
      success: (res) => {
        if (res.data === 0) {
          this.setData({
            address_get_type: 0,
            activeType: 0
          })
        } else if (res.data === 1) {
          this.setData({
            address_get_type: 1,
            activeType: 1
          })
        } else {
          const index = wx.getStorageSync('addressListTabIndex')
          this.setData({
            address_get_type: -1,
            activeType: index !== '' ? index : 0
          })
        }
        this.getInit();
      },
      fail: (res) => {
        console.log(res)
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  }
  ,

})